package logic;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;


/**
 * class for managing files data
 * @author Rekin
 *
 */
public class FilesManager {
	//public static String path = "file:///"+"root1/"+"private/"+"pkgame.txt";
	private static String path = System.getProperty("fileconn.dir.private")+"pkgame.txt";

	
//	public static String getRoot(){
//		Vector v = new Vector();
//		Enumeration e = FileSystemRegistry.listRoots();
//		while(e.hasMoreElements()){
//			v.addElement(e.nextElement().toString());
//		}
//		StringBuffer b = new StringBuffer();
//		for(int i=0;i<v.size();++i){
//			b.append(v.elementAt(i).toString()+" ");
//		}
////		Alert al = LogicHelper.makePopup("aaa", b.toString(), -1, AlertType.INFO);
////		MainMidlet.getMidletInstance().setDisplay(al);
//		return v.elementAt(v.size()-1).toString();
//	}
	
	/**
	 * @return date of creation the saved game file
	 */
	public static long getSavedGameDate() {
		long lastMod = -1;
		try{
			FileConnection fc = (FileConnection) Connector.open(path);
			if(!fc.exists()){
				return -1;
			}
			lastMod = fc.lastModified();
		}
		catch(IOException ee){
			ee.printStackTrace();
		}
		return lastMod;
	}
	
	/**
	 * saves data to file
	 * @param data data to be saved
	 * @param pathToFile path to file where data will be saved
	 * @return true if save was successful otherwise false
	 */
	public boolean saveToFile(String data, String pathToFile){
		try{
			FileConnection fc = (FileConnection) Connector.open(pathToFile);
			if(fc.exists()){
				fc.delete();
			}
			fc.create();
			DataOutputStream ds = fc.openDataOutputStream();
			ds.writeUTF(data);
			ds.close();
			fc.close();
			return true;
		}
		catch(IOException ee){
			ee.printStackTrace();
		}
		return false;
	}
	
	/**
	 * saves data to default file
	 * @param data data to be saved
	 * @return true if save was successful otherwise false
	 */
	public boolean saveToFile(String data){
		return saveToFile(data, path);
	}
	
	/**
	 * loads data from file
	 * @param pathToFile path to file
	 * @return String representing loaded file data
	 */
	public String loadFromFile(String pathToFile){
		String data=null;
		try{
			FileConnection fc = (FileConnection) Connector.open(pathToFile);
			if(!fc.exists()){
				return null;
			}
			DataInputStream is = fc.openDataInputStream();
			data = is.readUTF();
			is.close();
			fc.close();
		}
		catch(IOException ee){
			ee.printStackTrace();
		}
		return data;
	}
	
	/**
	 * loads data from default file
	 * @return String representing loaded file data
	 */
	public String loadFromFile(){
		return loadFromFile(path);
	}
}
